package cn.cool.middleware.dynamic_thread_pool.sdk.trigger.job;

import cn.cool.middleware.dynamic_thread_pool.sdk.domain.IDynamicThreadPoolService;
import cn.cool.middleware.dynamic_thread_pool.sdk.domain.model.entity.ThreadPoolConfigEntity;
import cn.cool.middleware.dynamic_thread_pool.sdk.registry.IRegistry;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.List;

@Slf4j
public class ThreadPoolDataReportJob {
    private final IDynamicThreadPoolService dynamicThreadPoolService;
    private final IRegistry registry;

    public ThreadPoolDataReportJob(IDynamicThreadPoolService dynamicThreadPoolService, IRegistry registry) {
        this.dynamicThreadPoolService = dynamicThreadPoolService;
        this.registry = registry;
    }

    @Scheduled(cron = "0/20 * * * * ?")
    public void execReportThreadPoolList() {
        List<ThreadPoolConfigEntity> threadPoolConfigEntityList = dynamicThreadPoolService.queryThreadPoolList();
        registry.reportThreadPool(threadPoolConfigEntityList);
        log.info("动态线程池，上报线程池信息：{}", JSONUtil.toJsonStr(threadPoolConfigEntityList));

        for (ThreadPoolConfigEntity threadPoolConfigEntity : threadPoolConfigEntityList) {
            registry.reportThreadPoolConfigParameter(threadPoolConfigEntity);
            log.info("动态线程池，上报线程池配置：{}", JSONUtil.toJsonStr(threadPoolConfigEntity));
        }
    }
}
